package com.hzjq.core.worker

import com.hzjq.core.BlastDelegate
import com.hzjq.core.callback.Callback
import com.hzjq.core.massage.DataMessageBean
import com.hzjq.core.receive.Receiver
import com.hzjq.core.receive.Receives
import com.hzjq.core.work.Work

class BackWork : Work<Boolean> {

    constructor(callback: Callback<Boolean>?) : super(callback)

    override fun cancel() {
        Receives.getInstance().unRegisterReceiver(
            BlastDelegate.getDelegate().getAssemblyCmdLoader()
                .backCmd()
        )
    }

    override fun doWork(vararg args: Any) {
        onProgressChanged(90, "正在返回...")
        val cmd = DataMessageBean(
            BlastDelegate.getDelegate().getAssemblyCmdLoader()
                .backCmd().cmd
        )
        Receives.getInstance().registerReceiver(BlastDelegate.getDelegate().getAssemblyCmdLoader()
            .backCmd(), object : Receiver {
            override fun convert(msg: String): Any {
                return msg
            }

            override fun onSuccess(msg: Any) {
                onProgressChanged(100, "返回成功")
                callback?.onResult(true)
            }

            override fun failed() {
                callback?.onResult(false)
            }

        })

        BlastDelegate.getDelegate().getCmdExeLoader()
            .exePollResultCmd(cmd.assembly(), callback)

    }


    fun backSimple(){
        val cmd = DataMessageBean(
            BlastDelegate.getDelegate().getAssemblyCmdLoader()
                .backCmd().cmd
        )
        Receives.getInstance().registerReceiver(BlastDelegate.getDelegate().getAssemblyCmdLoader()
            .backCmd(), object : Receiver {
            override fun convert(msg: String): Any {
                return msg
            }

            override fun onSuccess(msg: Any) {
                callback?.onResult(true)
                cancel()
            }

            override fun failed() {
                callback?.onResult(false)
                cancel()
            }

        })

        BlastDelegate.getDelegate().getCmdExeLoader()
            .exeOnceCmd(cmd.assembly(), callback)
    }
}